import Vue from 'vue'
import Vuex from 'vuex'

import { get_amazon_datas } from '@/api'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    token: sessionStorage.getItem('token') || '',
    headerInfo: JSON.parse(sessionStorage.getItem('headerInfo')) || ''
  },
  mutations: {
    SET_TOKEN: (state, token) => {
      sessionStorage.setItem('token', token)
      state.token = token
    },
    SET_HEADER_INFO: (state, info) => {
      sessionStorage.setItem('headerInfo', JSON.stringify(info))
      state.headerInfo = info
    }
  },
  actions: {
    /**
     * 登录
    */
    login ({ commit }, token) {
      return new Promise((resolve, reject) => {
        get_amazon_datas().then(res => {
          commit('SET_HEADER_INFO', res.account_health)
        })
        commit('SET_TOKEN', token)
        resolve()
      })
    },
    /**
     * 退出登录
    */
    logout ({ commit }) {
      return new Promise(resolve => {
        commit('removeToken')
        resolve()
      })
    }
  },
  modules: {
  }
})
